package com.monkey_pig.memoryalbum.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.monkey_pig.memoryalbum.R;
import com.monkey_pig.memoryalbum.activity.entity.ParentEntity;
import com.monkey_pig.memoryalbum.common.AppContext;

import java.util.List;

/**
 * Created by Junerver on 2016/5/23.
 * 这是ExpandableListView的配适器，直接使用AS重写了BaseExpandableListAdapter父类的方法
 */
public class MyExpandAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater = null;
    private List<ParentEntity> groupList;

      /**
       * 构造方法
       *
       * @param context
       * @param group_list
       */
    public MyExpandAdapter(Context context,   List<ParentEntity> group_list) {
        this.groupList = group_list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    /**
     * 返回一级Item总数
     */
    @Override
    public int getGroupCount() {
        return groupList.size();
    }


    /**
     * 返回二级Item总数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    /**
     * 获取一级Item内容
     */
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupList.get(groupPosition);
    }

    /**
     * 获取二级Item内容
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getChildList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        GroupViewHolder holder;
        //布局缓存
        if (convertView == null) {
            holder = new GroupViewHolder();
            //缓存布局文件
            convertView = inflater.inflate(R.layout.parent_status_item, null);
            //控件缓存
            holder.groupName = (TextView) convertView.findViewById(R.id.one_status_name);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        //操作控件
        holder.groupName.setText(groupList.get(groupPosition).getGroupName());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        //List<HashMap<String, ChildEntity>> entity = (List<HashMap<String, ChildEntity>>) getChild(groupPosition, childPosition);
        //缓存布局与控件
        if (convertView != null) {
            //存在缓存，从缓存中加载
            viewHolder = (ChildViewHolder) convertView.getTag();
        } else {
            //实例化viewholder
            viewHolder = new ChildViewHolder();
            //缓存布局文件
            convertView = inflater.inflate(R.layout.child_status_item, null);
            //缓存控件
            viewHolder.gridView = (GridView) convertView.findViewById(R.id.gview);

            convertView.setTag(viewHolder);
        }

        GridImageAdapter gridImageAdapter = new GridImageAdapter(AppContext.getContext(),groupList.get(groupPosition).getChildList());
        viewHolder.gridView.setAdapter(gridImageAdapter);

        //计算并设置gridView的高度
        final int rowHeightDp = 60;
        final float ROW_HEIGHT = AppContext.getContext().getResources()
                .getDisplayMetrics().density * rowHeightDp;
        double size = groupList.get(groupPosition).getChildList().size();
        int rowCount = (int) Math.ceil(size/2);
        final int GRID_HEIGHT = (int) (ROW_HEIGHT * rowCount);
        viewHolder.gridView.getLayoutParams().height = GRID_HEIGHT;
        //viewHolder.gridView.setOnItemClickListener(this);

        return convertView;
    }





    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }
    //一级item的viewholder
    private class GroupViewHolder {
        TextView groupName;
    }
    //二级item的viewholder
    private class ChildViewHolder {
        //二级item是一个gridview，在本适配器中新增一个适配器
        public GridView gridView;
    }
/**************************************************************************************************************************************/



}
